package com.choudou5.tools.excel.luowei;

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import com.choudou5.tools.excel.BasicExcelHelper;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class LwExcelExtractDemo extends BasicExcelHelper {

    public static void main(String[] args) throws Exception {
        String filePath = "E://data//工作簿2(1).xlsx";
        List<List<Object>> list = readData(filePath, 0);
        List<Map> newList = new ArrayList();
        Map map = null;
        String lastTitle = null;
        for (List<Object> row : list) {

//            System.out.println(JSON.toJSONString(row));

            String rowStr = row.get(0).toString();
            if(rowStr.startsWith("【")){
                if(map != null){
                    newList.add(map);
                }
                map = new LinkedHashMap();
                map.put("title", StrUtil.subAfter(rowStr, "】", false));
//                map.put("title", row.get(0).toString());
                continue;
            }
            if(map != null){
                if(rowStr.length() > 1){
                    lastTitle = rowStr;
                }
                if(rowStr.equals("危险级别")){
                    map.put("level", row.get(1));
                    continue;
                }
                boolean isContinue = multiRowParse(rowStr, "ip", "存在主机", lastTitle, row, map);
                if(isContinue)
                    continue;
                isContinue = multiRowParse(rowStr, "suggest", "修补建议", lastTitle, row, map);
                if(isContinue)
                    continue;
            }
        }
        if(map != null){ //处理最后一个
            newList.add(map);
        }
        //打印
        printData(newList);
        //输出文件
//        wirteFile("新文件01", newList);
    }

    private static boolean multiRowParse(String rowStr, String headKey, String headTitle, String lastTitle, List<Object> row, Map map){
        boolean isSameCell = row.size() == 2 && headTitle.equals(lastTitle);
        if(rowStr.equals(headTitle) || (isSameCell && map.containsKey(headKey))){
            if(isSameCell && map.containsKey(headKey)){
                map.put(headKey, map.get(headKey) +" "+ row.get(1));
            }else{
                map.put(headKey, row.get(1));
            }
            return true;
        }
        return false;
    }

    private static void wirteFile(String newFileName, List<Map> newList){
        File newFile = new File("E://data//"+newFileName+".xlsx");
        ExcelWriter writer = ExcelUtil.getWriter(newFile);
        Map<String, String> headerAlias = new LinkedHashMap<>();
        headerAlias.put("title", "漏洞名称");
        headerAlias.put("level", "危险级别");
        headerAlias.put("ip", "存在主机");
        headerAlias.put("suggest", "修补建议");
        writer.setHeaderAlias(headerAlias);
        writer.write(newList);
        writer.flush();
        writer.close();
    }


}
